package com.example.coreshopapp.keyboard.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.RelativeLayout;

import com.example.coreshopapp.R;
import com.example.coreshopapp.keyboard.adapter.KeyBoardAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 虚拟键盘
 */
public class CustomKeyboard extends RelativeLayout {
    Context context;
    //因为就6个输入框不会变了，用数组内存申请固定空间，比List省空间（自己认为）
    private GridView gridView;//用GrideView布局键盘，其实并不是真正的键盘，只是模拟键盘的功能
    private ArrayList<Map<String, String>> valueList; // 有人可能有疑问，为何这里不用数组了？
    // 因为要用Adapter中适配，用数组不能往adapter中填充
    private RelativeLayout layoutBack;
    public CustomKeyboard(Context context) {
        this(context, null);
    }

    public CustomKeyboard(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        View view = View.inflate(context, R.layout.layout_virtual_keyboard, null);//CustomKeyboard__style = 0,默认
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomKeyboard);
        if (typedArray != null){
            int mStyle = typedArray.getInt(R.styleable.CustomKeyboard_customkeyboard_style,0);
            typedArray.recycle();
            //Log.i("mNumKeyboard:","mStyle=" + mStyle);
            if (mStyle == 1){
                view = View.inflate(context, R.layout.layout_virtual_keyboard_center, null);
            } else if (mStyle == 2){
                view = View.inflate(context, R.layout.layout_virtual_keyboard_botton, null);
            }
        }
        valueList = new ArrayList<>();
        layoutBack = (RelativeLayout) view.findViewById(R.id.layoutBack);
        gridView = (GridView) view.findViewById(R.id.gv_keybord);
        initValueList();
        setupView();
        addView(view);      //必须要，不然不显示控件
    }

    public RelativeLayout getLayoutBack() {
        return layoutBack;
    }

    public ArrayList<Map<String, String>> getValueList() {
        return valueList;
    }

    private void initValueList() {
        // 初始化按钮上应该显示的数字
        for (int i = 1; i < 13; i++) {
            Map<String, String> map = new HashMap<>();
            if (i < 10) {
                map.put("name", String.valueOf(i));
            } else if (i == 10) {
                map.put("name", ".");
            } else if (i == 11) {
                map.put("name", String.valueOf(0));
            } else if (i == 12) {
                map.put("name", "");
            }
            valueList.add(map);
        }
    }

    public GridView getGridView() {
        return gridView;
    }

    private void setupView() {
        KeyBoardAdapter keyBoardAdapter = new KeyBoardAdapter(context, valueList);
        gridView.setAdapter(keyBoardAdapter);
    }

    /**
     * 2020-03-25Jonson
     * @param et_number
     * @param position
     * @param valueList
     */
    public static void OnNumKeyItemClick(EditText et_number, int position, ArrayList<Map<String, String>> valueList){
        try {
            String content = et_number.getText().toString().trim();
            if ((position == 9) && (content.contains("."))) return;//小数点，只能输入一个
            int mStar = 0;
            int mEnd = 0;
            if (et_number.isFocused()) {
                final int selStart = et_number.getSelectionStart();
                final int selEnd = et_number.getSelectionEnd();
                mStar = Math.max(0, Math.min(selStart, selEnd));
                mEnd = Math.max(0, Math.max(selStart, selEnd));
            }
            final CharSequence selectedText = et_number.getText().subSequence(mStar, mEnd);
            String selectStr = selectedText.toString();
            if (position < 11) {    //点击0~9按钮
                if ((mStar != mEnd) && (selectStr != "")) {
                    String mLeftStr = "";
                    if (mStar > 0) mLeftStr = content.substring(0, mStar);
                    String mRightStr = "";
                    if (mEnd < content.length())
                        mRightStr = content.substring(mEnd, content.length());
                    content = mLeftStr + valueList.get(position).get("name") + mRightStr;
                    et_number.setText(content);
                    et_number.setSelection(mStar + 1);
                } else {
                    int index = et_number.getSelectionStart();
                    Editable editable = et_number.getText();
                    editable.insert(index, valueList.get(position).get("name"));
                }
            }

            //点击退格键
            if (position == 11) {
                if (content.length() > 0) {
                    if ((mStar != mEnd) && (selectStr != "")) {
                        String mLeftStr = "";
                        if (mStar > 0) mLeftStr = content.substring(0, mStar);
                        String mRightStr = "";
                        if (mEnd < content.length())
                            mRightStr = content.substring(mEnd, content.length());
                        content = mLeftStr + mRightStr;
                        et_number.setText(content);
                        et_number.setSelection(mStar);
                    } else {
                        int index = et_number.getSelectionStart();
                        if (index > 0) {
                            Editable editable = et_number.getText();
                            editable.delete(index - 1, index);
                        }
                    }
                }
            }
        }
        catch(Exception x) {

        }
    }

}
